#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import wraps
from tools.log_control import INFO, WARNING


def log_decorator(switch: bool):
    """
    封装日志装饰器, 打印请求信息
    :param switch: 定义日志开关
    :return:
    """
    # 判断参数类型是否是 int 类型
    if isinstance(switch, bool):
        def decorator(func):
            @wraps(func)
            def swapper(*args, **kwargs):
                # 判断日志为开启状态，才打印日志
                res = func(*args, **kwargs)
                # 判断日志开关为开启状态
                if switch:
                    if res is not None:
                        _is_run = res['yaml_data']['is_run']
                        # 判断正常打印的日志，控制台输出绿色
                        if _is_run is None or _is_run is True:
                            INFO.logger.info(
                                "\n==================================="
                                "==============================================\n"
                                "测试标题: %s\n"
                                "请求方式: %s\n"
                                "请求头:   %s\n"
                                "请求路径: %s\n"
                                "请求内容: %s\n"
                                "接口响应内容: %s\n"
                                "数据库断言数据: %s\n"
                                "================================"
                                "=================================================",
                                res['yaml_data']['detail'],
                                res['yaml_data']['method'],
                                res['yaml_data']['headers'],
                                res['yaml_data']['url'],
                                res['yaml_data']['data'],
                                res['response_data'],
                                res['sql_data']
                            )
                        else:
                            # 跳过执行的用例，控制台输出黄色
                            WARNING.logger.warning(
                                "\n================================"
                                "=================================================\n"
                                "该条用例跳过执行.\n"
                                "测试标题: %s\n"
                                "请求方式: %s\n"
                                "请求头:   %s\n"
                                "请求路径: %s\n"
                                "请求内容: %s\n"
                                "接口响应内容: %s\n"
                                "数据库断言数据: %s\n"
                                "================================"
                                "=================================================",
                                res['yaml_data']['detail'],
                                res['yaml_data']['method'],
                                res['yaml_data']['headers'],
                                res['yaml_data']['url'],
                                res['yaml_data']['data'],
                                res['response_data'],
                                res['sql_data']
                            )
                    res_data = res
                else:
                    res_data = res
                return res_data
            return swapper
    else:
        raise TypeError("日志开关只能为 Ture 或者 False")

    return decorator
